<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 2015-11-21
 */
namespace Home\Controller;
use Home\Logic\UsersLogic;
use Think\Page;
use Think\Verify;

class UserController extends BaseController {

	public function _initialize() {
		parent::_initialize();
		$nologin = array('login', 'register', 'getcode', 'verifycode', 'register_ajax', 'login_ajax');

		if (CONTROLLER_NAME == "User") {
			if (!$this -> user && !in_array(ACTION_NAME, $nologin)) {
				header("location:" . U('Home/User/login'));
				exit ;
			} else {
				if ($this -> user && in_array(ACTION_NAME, $nologin)) {

					header("location:" . U('Home/Index/Index'));
				}

			}
		}

	}

	public function login() {
		$this -> display();

	}

	public function logout() {
		session_unset();
		session_destroy();
		$this -> ajaxReturn(array('msg' => '注销成功'));

	}

	public function register() {
		$this -> display();
	}

	public function ucenter() {
		$Users = new UsersLogic();
		$data = $Users -> ucenter();
		$this -> assign("user", $data);
		$this -> display();
	}

	public function myorder() {
		$Users = new UsersLogic();
		$list = $Users -> myorder();
		$this -> assign("list", $list);
		$this -> display();
	}

	//登陆
	public function login_ajax() {
		$username = I('post.username');
		$password = I('post.password');
		$Users = new UsersLogic();
		$ret = $Users -> login($username, $password);

		$this -> ajaxReturn($ret);
	}

	//注册
	public function register_ajax() {
		$username = I('post.username');
		$password = I('post.password');
		$checkcode = I('post.checkcode');
		$this -> verifycode($checkcode);
		$Users = new UsersLogic();
		$ret = $Users -> register($username, $password);
		$this -> ajaxReturn($ret);

	}

	//修改个人资料
	public function ucenter_ajax() {
		$data = $_POST;
		$Users = new UsersLogic();
		$ret = $Users -> ucenter_modify($data);
		$this -> ajaxReturn($ret);

	}

	//生成验证码
	public function getcode() {
		$config = array('fontSize' => 15, // 验证码字体大小
		'length' => 4, // 验证码位数
		'useNoise' => false, // 关闭验证码杂点
		);
		$Verify = new \Think\Verify($config);
		$Verify -> entry();
		//可指定登录与注册ID
	}

	//检测验证码
	public function verifycode($code) {
		$verify = new \Think\Verify();

		$ret = $verify -> check($code);
		if (!$ret) {
			$data['status'] = -1;
			$data['msg'] = '验证码错误';
			$this -> ajaxReturn($data);
			exit ;
		}

	}

}
